package com.example.infowall02.controller;

import com.example.infowall02.domain.Articles;
import com.example.infowall02.domain.LikeRequest;
import com.example.infowall02.domain.User;
import com.example.infowall02.mapper.ArticlesMapper;
import com.example.infowall02.mapper.LikeMapper;
import com.example.infowall02.mapper.UserMapper;
import com.example.infowall02.service.LikeService;
import com.example.infowall02.service.UserService;
import com.example.infowall02.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private LikeService likeService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private UserService userService;


    // 点赞文章
    @PutMapping
    public Result likeArticle(@RequestBody LikeRequest likeRequest) {
        int userId = likeRequest.getUserId();
        int articleId = likeRequest.getArticleId();
        // 检查用户否存在
        int userCount = userMapper.countUserById(userId);
        if (userCount == 0) {
            return Result.error("用户不存在，点赞失败");
        }
        // 检查文章是否存在
        int articleCount = articlesMapper.countArticleById(articleId);
        if (articleCount == 0) {
            return Result.error("文章不存在，点赞失败");
        }
        // 检查用户是否已经点过赞
        boolean isLike = likeService.isUserLikedArticle(userId, articleId);
        if (isLike) {
            return Result.error("你已经点过赞了，无需重复点赞");
        }
        // 用户和文章都存在，且未点过赞，执行点赞操作
        LikeRequest like = likeService.likeArticle(userId, articleId);
        return Result.success(like);
    }

    // 取消点赞文章
    @DeleteMapping
    public Result unlikeArticle(@RequestBody LikeRequest likeRequest) {
        int userId = likeRequest.getUserId();
        int articleId = likeRequest.getArticleId();
        // 检查用户否存在
        int userCount = userMapper.countUserById(userId);
        if (userCount == 0) {
            return Result.error("用户不存在，取消点赞失败");
        }
        // 检查文章是否存在
        int articleCount = articlesMapper.countArticleById(articleId);
        if (articleCount == 0) {
            return Result.error("文章不存在，取消点赞失败");
        }
        // 检查用户是否已经点过赞
        boolean isLike = likeService.isUserLikedArticle(userId, articleId);
        if (isLike) {
            likeService.unlikeArticle(userId, articleId);
            return Result.success("成功取消点赞");
        }
        // 用户和文章都存在，且未点过赞，执行点赞操作
        return Result.error("你还没有点过赞，无法取消点赞");
    }

    // 获取用户点赞数
    @GetMapping("/count/{userId}")
    public Result countUser(@PathVariable int userId) {
        // 检查用户否存在
        User user = userService.findUserById(userId);
        if(user == null) {
            return Result.error("用户不存在，获取失败");
        }
        // 获取用户点赞数
        int count = likeService.getUserLikeCount(userId);
        return Result.success(count);
    }
    //获取用户的点赞文章列表
    @GetMapping("/{userId}")
    public Result<List<Articles>> userLikedArticleList(@PathVariable int userId) {
        User user = userService.findUserById(userId);
        if(user!=null){
            List<Articles> articles = likeService.getUserLikedArticles(userId);
            return Result.success(articles);
        }
        return Result.error("用户不存在，获取失败");
    }
}



